package com.ogawa.projectcommon.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.ogawa.projectcommon.R

import kotlinx.android.synthetic.main.layout_width_view.view.*

class WidthLevelView : FrameLayout, View.OnClickListener {
    private var lev = 0

    constructor(context: Context) : super(context)

    @SuppressLint("CustomViewStyleable")
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        LayoutInflater.from(context).inflate(R.layout.layout_width_view, this)
        tv_width.setOnClickListener(this)
        tv_medium.setOnClickListener(this)
        tv_narrow.setOnClickListener(this)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    @SuppressLint("SetTextI18n")
    fun setLev(lev: Int) {
        tv_width.isSelected = lev == 1
        tv_medium.isSelected = lev == 2
        tv_narrow.isSelected = lev == 3
        if (lev == 0) {
            tv_lev.text = context.getString(R.string.unadjustable)
            group_lev.visibility = View.VISIBLE
        } else {
            group_lev.visibility = View.GONE
        }
        this.lev = lev
    }

    fun setTitle(title: String) {
        tv_lev_text.text = title
    }

    override fun onClick(v: View?) {
        if (lev == 0) {
            return
        }
        when (v?.id) {
            R.id.tv_width -> setLev(1)
            R.id.tv_medium -> setLev(2)
            R.id.tv_narrow -> setLev(3)
        }
    }

    fun getLev(): Int {
        return lev
    }
}